package com.cloudptt.wp.ui;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.InputFilter;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.text.InputType;
import android.text.method.DigitsKeyListener;
import android.widget.Toast;

import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ui.address.workspace.IFuncDlgClick;
import com.cloudptt.wp.ui.message.adapter.GroupMemberAdapter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UiDlg
{

	/**
	 * 寰楀埌鑷畾涔夌殑progressDialog
	 * @param context
	 * @param msg
	 * @return
	 */
	public static Dialog createLoadingDialog(Context context, String msg) {

		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.loading_dialog, null);// 寰楀埌鍔犺浇view
		LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 鍔犺浇甯冨眬
		// main.xml涓殑ImageView
		ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
		TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 鎻愮ず鏂囧瓧
		// 鍔犺浇鍔ㄧ敾
		Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
				context, R.anim.loading_animation);
		// 浣跨敤ImageView鏄剧ず鍔ㄧ敾
		spaceshipImage.startAnimation(hyperspaceJumpAnimation);
		tipTextView.setText(msg);// 璁剧疆鍔犺浇淇℃伅

		Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 鍒涘缓鑷畾涔夋牱寮廳ialog

		loadingDialog.setCancelable(false);// 涓嶅彲浠ョ敤鈥滆繑鍥為敭鈥濆彇娑�
		loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));// 璁剧疆甯冨眬
		loadingDialog.show();
		return loadingDialog;

	}

	private static AlertDialog alertdialog;
	private static DialogInterface.OnClickListener localListener;
	public static void showListDialog(Context context,String[] listName,DialogInterface.OnClickListener listener)
	{
		localListener = listener;
		LayoutInflater inflater = (LayoutInflater)context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
		View view = inflater.inflate(R.layout.alertdialog_simple_list,null);
		ListView lv  = (ListView)view.findViewById(R.id.test_alertdialog_listview);
		lv.setAdapter(new ArrayAdapter(context,R.layout.it_dialog_view,listName));
		lv.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {
				alertdialog.dismiss();
				localListener.onClick( alertdialog, position );
			}

		});
		alertdialog = new AlertDialog.Builder(context, R.style.dialog_exit)
				.create();
		alertdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

		Window window = alertdialog.getWindow();
		if (alertdialog != null && window != null) {
			WindowManager.LayoutParams attr = window.getAttributes();
			if (attr != null) {
				attr.height = ViewGroup.LayoutParams.WRAP_CONTENT;
				attr.width = ViewGroup.LayoutParams.WRAP_CONTENT;
				attr.gravity = Gravity.CENTER;//设置dialog 在布局中的位置
			}
			window.setAttributes(attr);
		}

		alertdialog.show();
		alertdialog.setContentView( view );
//		alertdialog.getWindow( ).setLayout( 500, 300 );
	}

	public static AlertDialog getAdapterListDialog(Context context, GroupMemberAdapter adapter)
	{
		LayoutInflater inflater = (LayoutInflater)context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
		View view = inflater.inflate(R.layout.alertdialog_simple_list,null);
		ListView lv  = (ListView)view.findViewById(R.id.test_alertdialog_listview);
		lv.setAdapter(adapter);

		alertdialog = new AlertDialog.Builder(context, R.style.CustomDialogTheme)
				.create();
		alertdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		alertdialog.setCanceledOnTouchOutside(true);
		alertdialog.setCancelable(true);
		alertdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

		Window window = alertdialog.getWindow();
		window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

		alertdialog.create();
		alertdialog.setContentView( view );
		return alertdialog;
	}

	private static Dialog warningDialog;

	public static Dialog createWarningDialog(Context context, String warningString, int drawableId){
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.warning_dialog, null);
		ImageView warningImage = (ImageView) v.findViewById(R.id.warning_img);
		TextView messageTV = (TextView) v.findViewById(R.id.warning_name);
		if(drawableId == 0){
			warningImage.setImageResource(R.drawable.ptticon);
		}else{
			warningImage.setImageResource(drawableId);
		}
		messageTV.setText(warningString);

		warningDialog = new Dialog(context, R.style.loading_dialog);

		Button confirmBtn = (Button)v.findViewById(R.id.warning_confirm_btn);

		confirmBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				warningDialog.dismiss();
			}
		});

		AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
		builder.setView(v);
		warningDialog = builder.create();
		return warningDialog;
	}
	private static AlertDialog funcDlg = null;
	public static void showFuncDialog(Context context, int type,String title, IFuncDlgClick listener) {
		/*@setView 装入一个EditView
		 */
		EditText editText = new EditText(context);
		editText.setInputType(InputType.TYPE_CLASS_NUMBER);
		InputFilter[] filters = new InputFilter[] { new InputFilter.LengthFilter(30) };
		editText.setFilters(filters);
		editText.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
		DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				funcDlg.dismiss();
			}
		};

		final AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(context);
		dlgBuilder.setTitle(title).setView(editText);
		dlgBuilder.setPositiveButton(R.string.confirm,null);
		dlgBuilder.setNegativeButton(R.string.cancel,cancelListener);
		funcDlg = dlgBuilder.create();
		funcDlg.show();
		funcDlg.getButton(AlertDialog.BUTTON_POSITIVE)
				.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if(listener != null) {
							String val =editText.getText().toString();
							if(val == null
									|| val.trim().equals(""))
							{
								String errStr = context.getString(R.string.input_null);
								CommDlg.show(context, errStr, Toast.LENGTH_LONG);
								return;
							}
							listener.onClickOk(type,val,"");
						}
						if(funcDlg != null)
						{
							funcDlg.dismiss();
						}
					}
				});
	}

	public static void showFuncDialog2(Context context, int type,String title, IFuncDlgClick listener) {

		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.dlg_func, null);
		EditText editText1 = (EditText) v.findViewById(R.id.editText1);
		InputFilter[] filters = new InputFilter[] { new InputFilter.LengthFilter(30) };
		editText1.setFilters(filters);
		EditText editText2 = (EditText) v.findViewById(R.id.editText2);
		editText2.setFilters(filters);

		warningDialog = new Dialog(context, R.style.loading_dialog);
		DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				funcDlg.dismiss();
			}
		};

		final AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(context);
		dlgBuilder.setTitle(title).setView(v);
		dlgBuilder.setPositiveButton(R.string.confirm,null);
		dlgBuilder.setNegativeButton(R.string.cancel,cancelListener);
		funcDlg = dlgBuilder.create();
		funcDlg.show();
		funcDlg.getButton(AlertDialog.BUTTON_POSITIVE)
				.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if(listener != null) {
							String val1 =editText1.getText().toString();
							if(val1 == null
									|| val1.trim().equals(""))
							{
								String welcomeMessage = context.getString(R.string.input_null);
								CommDlg.show(context, welcomeMessage, Toast.LENGTH_LONG);
								return;
							}
							String val2 =editText2.getText().toString();
							if(val2 == null
									|| val2.trim().equals(""))
							{
								String welcomeMessage = context.getString(R.string.input_null);
								CommDlg.show(context, welcomeMessage, Toast.LENGTH_LONG);
								return;
							}
							listener.onClickOk(type,val1,val2);
						}
						if(funcDlg != null)
						{
							funcDlg.dismiss();
						}
					}
				});
	}

	public static void showFuncDialog3(Context context, int type,String title, IFuncDlgClick listener) {

		EditText editText = new EditText(context);
		editText.setInputType(InputType.TYPE_CLASS_NUMBER);
		InputFilter[] filters = new InputFilter[] { new InputFilter.LengthFilter(30) };
		editText.setFilters(filters);
		editText.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
		DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				funcDlg.dismiss();
			}
		};

		final AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(context);
		dlgBuilder.setTitle(title).setView(editText);
		dlgBuilder.setNegativeButton(R.string.single_call_voice,null);
		dlgBuilder.setPositiveButton(R.string.single_call_video,null);
		dlgBuilder.setNeutralButton(R.string.cancel,cancelListener);

		funcDlg = dlgBuilder.create();
		funcDlg.show();
		funcDlg.getButton(AlertDialog.BUTTON_NEGATIVE)
				.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if(listener != null) {
							String val1 =editText.getText().toString();
							if(val1 == null
									|| val1.trim().equals(""))
							{
								String welcomeMessage = context.getString(R.string.input_null);
								CommDlg.show(context, welcomeMessage, Toast.LENGTH_LONG);
								return;
							}

							listener.onClickOk(type,val1,"VOICE");
						}
						if(funcDlg != null)
						{
							funcDlg.dismiss();
						}
					}
				});
		funcDlg.getButton(AlertDialog.BUTTON_POSITIVE)
				.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if(listener != null) {
							String val1 =editText.getText().toString();
							if(val1 == null
									|| val1.trim().equals(""))
							{
								String welcomeMessage = context.getString(R.string.input_null);
								CommDlg.show(context, welcomeMessage, Toast.LENGTH_LONG);
								return;
							}
							listener.onClickOk(type,val1,"VIDEO");
						}
						if(funcDlg != null)
						{
							funcDlg.dismiss();
						}
					}
				});
	}
}
